//
//  NewFeatureViewController.swift
//  glzbowei
//
//  Created by 高伦展 on 2017/12/26.
//  Copyright © 2017年 glz. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

private let imageCount = 4

class NewFeatureViewController: UICollectionViewController {

    // MARK: - 构造函数
    init(){
        //super.指定的构造函数
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.main.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.isPagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var prefersStatusBarHidden: Bool{
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.register(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
 
    }
 

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

   
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! NewFeatureCell
    
        // Configure the cell
        cell.imgIndex = indexPath.item
    
        return cell
    }
    
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page != imageCount - 1
        {
            return
        }
        
        let cell = collectionView?.cellForItem(at: NSIndexPath(item: page, section: 0) as IndexPath) as! NewFeatureCell
        
         cell.showBtnAim()
        
    }
}

private class NewFeatureCell :UICollectionViewCell{
    
     var imgIndex:Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imgIndex+1)")
            
            startBtn.isHidden = true
        }
    }
    
    @objc func clickStartBtn(){
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBSwitchRootViewControllerNotification), object: nil)
    }
    
    func showBtnAim(){
        startBtn.isHidden = false
        startBtn.transform = CGAffineTransform().scaledBy(x: 0, y: 0)
        startBtn.isUserInteractionEnabled = false
        
        UIView.animate(withDuration: 1.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: {
            self.startBtn.transform = CGAffineTransform.identity
        }) { (_) in
            self.startBtn.isUserInteractionEnabled = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI()
    {
        addSubview(iconView)
        addSubview(startBtn)
        
        iconView.frame = bounds
        
        startBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.bottom.equalTo(self.snp.bottom).multipliedBy(0.7)
        }
        
        startBtn.addTarget(self, action: #selector(NewFeatureCell.clickStartBtn), for: .touchUpInside)
    }
    
    //图像
    private lazy var iconView :UIImageView = UIImageView()
    //体验按钮
    private lazy var startBtn :UIButton = UIButton(title: "体验按钮", color: UIColor.white, backImgName: "new_feature_finish_button")
    
}
